package com.gxa.modules.service.impl;


import com.gxa.common.utils.DateUtils;
import com.gxa.common.vo.JsonResult;
import com.gxa.common.vo.PageRecv;
import com.gxa.common.vo.ResultVo;
import com.gxa.modules.mapper.Loanafter_PayBackMapper;
import com.gxa.modules.pojo.LoanSearch;
import com.gxa.modules.pojo.Loanafter_Payback;

import com.gxa.modules.service.Loanafter_PayBackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * Created with IntelliJ IDEA.
 * User: 张剑宇
 * Date: 2021/12/27 10:00
 * Description:
 * Version: V1.0
 */
@Service
public class Loanafter_PayBackServiceImpl implements Loanafter_PayBackService {
    @Autowired(required=false)
    private Loanafter_PayBackMapper loanafter_PayBackMapper;


    @Override

    //正常还款查询
    public JsonResult queryPayBack(PageRecv<LoanSearch> pageRecv) {
        //对查询页面参数计算
        Integer currentPage=pageRecv.getCurrentPage();
        Integer pageSize=pageRecv.getPageSize();
        Integer limit1=(currentPage-1)*pageSize;
        pageRecv.setCurrentPage(limit1);

        //调用Mapper方法获取返回结果
        List<Loanafter_Payback> list=loanafter_PayBackMapper.queryPayBack(pageRecv);
        Integer count=loanafter_PayBackMapper.queryPayBackCount(pageRecv.getData());

        //将结果塞入R
        JsonResult r=new JsonResult();
        r.setData(list);
        r.setCode(200);
        r.setMsg("查询成功！");
        r.setCount(count);
//        addPayBack("2021-12-22",new BigDecimal("11.2"),new BigDecimal("3000"),3,10002);

        return r;
    }

    //提前还款查询
    public ResultVo queryEarlyPayBack(LoanSearch loanSearch) {

        Loanafter_Payback loanafter_payback=loanafter_PayBackMapper.countEarlyTotal(loanSearch);

        //将结果塞入R
        ResultVo r=new ResultVo();
        r.setData(loanafter_payback);
        r.setCode(200);
        r.setMsg("查询成功！");




        return r;
    }


    @Override
    public ResultVo addPayBack(String startdate, BigDecimal interestrate, BigDecimal principle,Integer timelimit,Integer loan_id) {

        //计算每月应还本金
        BigDecimal timelimit1=new BigDecimal(timelimit);
        BigDecimal payback_principle=principle.divide(timelimit1,2,BigDecimal.ROUND_HALF_UP);
        System.out.println("计算每月应还本金"+payback_principle);
        //计算每月应还利息
        BigDecimal number = new BigDecimal(100);
        BigDecimal payback_interest=payback_principle.multiply(interestrate).divide(number);
        System.out.println("计算每月应还利息"+payback_interest);
        //计算每月应还总额
        BigDecimal payback_allpay=payback_principle.add(payback_interest);
        System.out.println("计算每月应还总额"+payback_allpay);
        //计算总共应还金额
        BigDecimal allpay=principle.add(principle.multiply(interestrate).divide(number));
        System.out.println("计算总共应还金额"+allpay);

        Loanafter_Payback loanafter_payback=new Loanafter_Payback();
        List list=new ArrayList();
        for (int i = 1; i <=timelimit ; i++) {
            //将还款日期增加一个月
            startdate=DateUtils.addMothToDate(1,startdate,"yyyy-MM-dd");
            //将初始还款信息塞入对象
            loanafter_payback.setLoan_id(loan_id);
            loanafter_payback.setPayback_paydate(startdate);
            loanafter_payback.setPayback_allpay(payback_allpay);
            loanafter_payback.setPayback_principle(payback_principle);
            loanafter_payback.setPayback_interest(payback_interest);
            allpay=allpay.subtract(payback_allpay);
            loanafter_payback.setPayback_rest(allpay);
            loanafter_payback.setPayback_timelimit(i);
            loanafter_PayBackMapper.addPayBack(loanafter_payback);
            list.add(loanafter_payback.getPayback_id());


        }


        ResultVo r=new ResultVo();
        r.setCode(200);
        r.setMsg("添加成功");
        return r;
    }

    @Override
    public ResultVo updatePayBack(Loanafter_Payback loanafter_payback) {
        loanafter_PayBackMapper.updatePayBack(loanafter_payback);
        ResultVo r=new ResultVo();
        r.setCode(200);
        r.setMsg("修改成功");
        return r;
    }
    @Override
    public ResultVo updateEarlyPayBack(LoanSearch loanSearch) {
        List<Loanafter_Payback> list=loanafter_PayBackMapper.queryChangeRate(loanSearch);
        BigDecimal number = new BigDecimal(100);
        for (Loanafter_Payback one:list
             ) {
            //计算变更前的利率
            BigDecimal rate=one.getPayback_interest().divide(one.getPayback_principle(),4,BigDecimal.ROUND_HALF_UP);
            System.out.println("利率"+rate);
            //计算变更后的剩余应还rest
            one.setPayback_rest(one.getPayback_rest().divide(new BigDecimal("1").add(rate),3,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("1").add(new BigDecimal(loanSearch.getCompound_interest()+"").divide(number))));
            //计算利率变更后的利息
            one.setPayback_interest(one.getPayback_principle().multiply(new BigDecimal(loanSearch.getCompound_interest()+"")).divide(number));
            //计算利率变更后的allpay
            one.setPayback_allpay(one.getPayback_principle().add(one.getPayback_interest()));
            loanafter_PayBackMapper.updatePayBack(one);
        }
        ResultVo r=new ResultVo();
        r.setCode(200);
        r.setMsg("修改成功");
        return r;
    }

    @Override
    public JsonResult queryPayBackLoan(PageRecv<LoanSearch> pageRecv) {
        //对查询页面参数计算
        Integer currentPage=pageRecv.getCurrentPage();
        Integer pageSize=pageRecv.getPageSize();
        Integer limit1=(currentPage-1)*pageSize;
        pageRecv.setCurrentPage(limit1);

        //调用Mapper方法获取返回结果
        List<Map> list=loanafter_PayBackMapper.queryPayBackLoan(pageRecv);
        Integer count=loanafter_PayBackMapper.queryPayBackCountLoan(pageRecv.getData());

        //将结果塞入R
        JsonResult r=new JsonResult();
        r.setData(list);
        r.setCode(200);
        r.setMsg("查询成功！");
        r.setCount(count);
//        addPayBack("2021-12-22",new BigDecimal("11.2"),new BigDecimal("3000"),3,10002);

        return r;
    }

}
